var vote= new Vue({
    el : '#Votelist',
    data:{
        userId : $("#id").val(),
        xiangmuId:$("#xiangmuId").val(),
        xiangmuleixing:$("#xiangmuleixing").val(),
        detailid: "",
        shiyongyuanze:{},
        yiyongyuanze:{},
        syyz_value: [],
        shiyongyuanzelist:[],
        xiangmu : {},
        votelist:[],
        show : false,
        issubmited:false,
        total:'',
        tongyishu:0,
        butongyishu:0
    },
    created : function() {
        var _this = this;

        $(".nav navbar-nav li").removeClass("active");
        $("#nav_biaojue").addClass("active");
        _this.findIsacticed();

    },
    methods : {
        openSelect() {
            console.log(123)
            this.$refs.selectRef.toggleMenu();
        },
        handleCheckAllChange(val) {
            var ids = this.shiyongyuanzelist.map(item => {
                return item.id
            })
            this.syyz_value = val ? ids : [];
            this.isIndeterminate = false;
            console.log(this.checkedCities)
        },
        handleCheckedCitiesChange(value) {
            var ids = this.shiyongyuanzelist.map(item => {
                return item.id
            })
            let checkedCount = value.length;
            this.checkAll = checkedCount === ids.length;
            this.isIndeterminate = checkedCount > 0 && checkedCount < ids.length;
            this.syyz_value = value
        },
        initDict : function() {

            var _this = this;
            $.post("/dict_shiyongyuanze/find_all", {
                text:"",
                rdm : Math.random()
            }, function (ppData) {
                if (ppData != null) {
                    if (ppData.result == '1') {
                        _this.shiyongyuanzelist = ppData.resultContent;
                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
        },
        syyzChange:function (val){
            this.syyz_value = val
        },

        bindShiyongyuanze:function (){
            var _this = this;
            layer.open({type:3});
            $.post('/yuanze/find_one', {
                detailid : _this.detailid,
                xiangmuid: _this.xiangmuId,
                userid:_this.userId,
                rdm : Math.random()
            },function(ppData) {
                layer.closeAll("loading");
                if(ppData != null){
                    if(ppData.result == "1"){
                        _this.syyz_value = []
                        var data = ppData.resultContent;
                        _this.yiyongyuanze = data;
                        _this.initDict();
                        if(data.length > 0)
                        {
                            _this.syyz_value = ppData.resultContent[0].shiyongyuanzeid.split(',')

                        }else
                        {
                            _this.yiyongyuanze.shiyongyuanze=[];
                        }
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },

        modifyShiyongyuanze:function (){
            var _this = this;

           /* if(_this.syyz_value.join(',')==""){
                layer.alert("请选择适用原则再保存。");
                return false;
            }*/
            var shiyongyuanze=_this.syyz_value.join(',');


            $.post('/yuanze/save', {
                detailid : _this.detailid,
                xiangmuid: _this.xiangmuId,
                userid:_this.userId,
                shiyongyuanzeid: _this.syyz_value.join(','),
                rdm : Math.random()
            },function(ppData) {
                layer.closeAll("loading");
                if(ppData != null){
                    if(ppData.result == "1"){
                        layer.open({
                            time:1000,
                            btn:[],
                            content:"保存成功!",
                        });
                        $("#editShiyongyuanzeModal").modal("hide");
                        _this.getYinjinResultCount();
                        $("#yuanze_"+_this.detailid).html(shiyongyuanze);
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },

        checkIsSubmited:function (){
            var _this = this;
            layer.open({type:3});
            $.post('/renyuanfenpei/checkIsSubmited', {
                xiangmuid :_this.xiangmuId,
                userid :_this.userId,
                rdm : Math.random()
            },function(ppData) {
                layer.closeAll("loading");
                if(ppData != null){
                    if(ppData.result == "1"){
                        if(ppData.resultContent=="1"){
                            _this.issubmited=true;
                        }
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },
        tosubmit:function (){
            var _this = this;
            layer.open({type:3});
            $.post('/renyuanfenpei/submit', {
                xiangmuid :_this.xiangmuId,
                userid :_this.userId,
                rdm : Math.random()
            },function(ppData) {
                layer.closeAll("loading");
                if(ppData != null){
                    if(ppData.result == "1"){
                        _this.issubmited=true;
                        layer.alert("提交成功");
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },

        findIsacticed:function (){
            var _this = this;
            layer.open({type:3});
            $.post('/xiangmu/getIsactivedxiangmu', {
                rdm : Math.random()
            },function(ppData) {
                layer.closeAll("loading");
                if(ppData != null){
                    if(ppData.result == "1"){
                        var data = ppData.resultContent;
                        _this.xiangmu = data;
                        _this.xiangmuId=_this.xiangmu.xiangmuid;
                        _this.findBiaojuelist();
                        _this.checkIsSubmited();
                        _this.getYinjinResultCount();
                        _this.initDict();
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },

        findBiaojuelist:function (){
            var _this = this;
            layer.open({type:3});
            $.post("/yinjin/findBiaojuelist",{
                xiangmuid :_this.xiangmuId,
                userid :_this.userId,
                rdm : Math.random()
            },function(ppData){
                layer.closeAll("loading");
                if (ppData != null){
                    if(ppData.result == "1"){
                        var data=ppData.resultContent;

                        if(data.length > 0){
                            _this.show = true;
                            _this.votelist=data;
                            _this.total=data.length;
                            _this.getYinjinResultCount();
                        }else {
                            _this.show = false;
                        }

                    }else{
                        layer.alert(ppData.message);
                    }
                }

            },"json");
        },

        getYinjinResultCount:function (){
            var _this = this;
            layer.open({type:3});
            $.post('/jieguo/getYinjinResultCount', {
                xiangmuid :_this.xiangmuId,
                userid :_this.userId,
                rdm : Math.random()
            },function(ppData) {
                layer.closeAll("loading");
                if(ppData != null){
                    if(ppData.result == "1"){
                        var data = ppData.resultContent;
                        _this.tongyishu=data.tongyiShu;
                        _this.butongyishu=data.butongyiShu;
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },

        toModify(ppDetailid,ppNumber){
            $('#editShiyongyuanzeModal').modal();
            $("#myModalLabel_shiyongyuanze").html("序号："+ppNumber+"选择理由");
            var _this = this;
            _this.detailid=ppDetailid;
            _this.bindShiyongyuanze();
        },

        toVote:function (ppId,ppIstongyi) {

            var _this = this;
            layer.open({type: 3});
            $.post('/jieguo/toupiao', {
                id: ppId,
                userid: _this.userId,
                result: ppIstongyi,
                rdm: Math.random()
            }, function (ppData) {
                layer.closeAll("loading");
                if (ppData != null) {
                    if (ppData.result == "1") {
                        _this.getYinjinResultCount();
                        if (ppIstongyi == 1) {
                            $("#jieguo" + ppId).html("<span style=\"color:green\">同意</span>");
                        }
                        if (ppIstongyi == 0) {
                            $("#jieguo" + ppId).html("<span style=\"color:red\">不同意</span>");
                        }
                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
        },

        toVoteAll:function (ppresult){

            var _this = this;
            var mmMessage="";
            if("0"==ppresult){
                mmMessage="确定全部不同意当前表决项？";
            }else if("1"==ppresult){
                mmMessage="确定全部同意当前表决项？";
            }
            layer.confirm(mmMessage,{
                btn : ['是','否']
            },function(){
                layer.open({type:3});

                $.post("/jieguo/toupiao_all", {
                    xiangmuid :_this.xiangmuId,
                    userid :_this.userId,
                    result:ppresult,
                    random : Math.random()
                }, function(ppData) {
                    if (ppData != null) {
                        layer.closeAll("loading");

                        if(ppData.result != "1"){
                            layer.alert(ppData.message);
                        }else{
                            layer.open({
                                time:1000,
                                btn:[],
                                content:"操作成功!",
                            });
                            window.location.reload();
                            //_this.getBaofeiResultCount();
                            // _this.findBiaojuelist();
                        }
                    }
                },"json");
            })
        },
        toshowDetail :function (ppdetailId){
            layer.alert(ppdetailId);
        },



    }
})